package com.meitianhui.convenitenbuy.module.wallet.presenter;

import com.google.gson.Gson;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.base.BaseResponse;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.http.ResponseObserver;
import com.meitianhui.convenitenbuy.main.model.bean.UserToken;
import com.meitianhui.convenitenbuy.module.wallet.contract.WalletPaymentContract;
import com.meitianhui.convenitenbuy.module.wallet.http.WalletApiDataSource;
import com.meitianhui.convenitenbuy.module.wallet.model.PaymentCodeBean;
import com.meitianhui.convenitenbuy.module.wallet.model.UsableChangeBean;
import com.meitianhui.convenitenbuy.module.wallet.ui.activity.WalletPaymentActivity;

import java.util.HashMap;
import java.util.Map;


/**
 * Created by Administrator on 2017/11/2.
 */

public class WalletPaymentPresenter implements WalletPaymentContract.Presenter {

    private WalletPaymentActivity context;
    private final WalletPaymentContract.View view;

    public WalletPaymentPresenter(WalletPaymentActivity view,
                                  WalletPaymentActivity context) {
        this.view = view;
        this.context = context;
    }

    @Override
    public void attachView(Object view) {
    }

    @Override
    public void detachView() {
    }

    @Override
    public void getUsableChange() {
        String user_token = InfoProvider.getUserToken(App.getInstance()).getUser_token();
        String service = "finance.usableCashBalanceFind";

        Map<String, String> map = new HashMap<>();
        map.put("member_id", InfoProvider.getUserToken(App.getInstance()).getMember_id());
        map.put("member_type_key", "consumer");
        String jsonParams = new Gson().toJson(map);

        WalletApiDataSource.getInstance()
                .getUsableChange(user_token, service, jsonParams)
                .subscribe(new ResponseObserver<BaseResponse<UsableChangeBean>>(App.getInstance()) {
                    @Override
                    public void onSuccess(BaseResponse<UsableChangeBean> response) {
                        view.getUsableBalanceBack(response.data);
                    }
                });
    }

    @Override
    public void getUserSecurityCode() {
        UserToken userToken = InfoProvider.getUserToken(App.getInstance());
        String user_token = userToken.getUser_token();
        String service = "trade.tradeCodeRegister";

        Map<String, String> maps = new HashMap<>();
        maps.put("member_id", userToken.getMember_id());
        maps.put("member_type_key", "consumer");
        maps.put("type", "T");
        String jsonParams = new Gson().toJson(maps);

        WalletApiDataSource.getInstance()
                .getSecurityCode(user_token, service, jsonParams)
                .subscribe(new ResponseObserver<BaseResponse<PaymentCodeBean>>(App.getInstance()) {
                    @Override
                    public void onSuccess(BaseResponse<PaymentCodeBean> response) {
                        view.getSecurityCodeBack(response.data);
                    }
                });
    }

}
